﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using PTCapture.Support;
using Microsoft.Expression.Encoder.Devices;
using PTCapture.PatientInfo;

namespace PTCapture.Forms
{
    public partial class MainForm : Form
    {
        #region Fields

        /// <summary>
        /// Folder browser
        /// </summary>
        private FolderBrowserDialog folderBrowserDialog1;

        /// <summary>
        /// Exercise management form
        /// </summary>
        private ExerciseManagement exerciseManagement;

        #endregion

        #region Initialization

        /// <summary>
        /// Constructor
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            // Create components
            folderBrowserDialog1 = new FolderBrowserDialog();
            folderBrowserDialog1.ShowNewFolderButton = true;

            patients1.LoadPatientXML();
            ((Control)this.tpCameras).Enabled = false;
            ((Control)this.tpPlayBack).Enabled = false;
            ((Control)this.tpSnapshots).Enabled = false;

            // Create root directory
            if (!Directory.Exists(App.Default.RootPath))
            {
                Directory.CreateDirectory(App.Default.RootPath);
            }

            // Set the callback from user controls
            patients1.PatientSelected += new UserControls.PatientControl.customHandler(patients1_AssignPaths);
            cameraRecorderControl1.CameraStatusChanged += new UserControls.CameraRecorderControl.customHandler(mainForm_ChangeCameraButtonStatus);
            cameraRecorderControl2.CameraStatusChanged += new UserControls.CameraRecorderControl.customHandler(mainForm_ChangeCameraButtonStatus);
            moviePlayerUIControl1.SnapshotTaken += new UserControls.MoviePlayerUIControl.customHandler(moviePlayerUIControl1_AssignImages);
            snapshotDrawer1.MadeExercise += new UserControls.SnapshotDrawer.customHandler(snapshotDrawer1_MadeExercise);

            // Find available a/v devices
            GlobalVariables.videoDevices = EncoderDevices.FindDevices(EncoderDeviceType.Video);
            GlobalVariables.audioDevices = EncoderDevices.FindDevices(EncoderDeviceType.Audio);

            // Control events
            cbPlayback1.SelectedIndexChanged += new EventHandler(cbPlayback_SelectedIndexChanged);
            cbPlayback2.SelectedIndexChanged += new EventHandler(cbPlayback_SelectedIndexChanged);

            // Drag/drop 
            lbImages.MouseDown += new MouseEventHandler(lbImages_MouseDown);

            // Load exercises
            ExerciseSupport.LoadExerciseXML();
            exerciseManagement = new ExerciseManagement();
            exerciseManagement.RebindExercises();
            exerciseManagement.MadeExercise += new ExerciseManagement.customHandler(exerciseManagement_MadeExercise);

            // Release playback controls when finished.
            this.FormClosing += new FormClosingEventHandler(MainForm_FormClosing);
        }

        #endregion

        #region DelegateCalls

        /// <summary>
        /// When exercise is changed, update patient screen.
        /// </summary>
        /// <returns></returns>
        bool exerciseManagement_MadeExercise()
        {
            patients1.RebindExercises();

            return true;
        }

        #endregion

        #region Form Events

        /// <summary>
        /// Release events on closing.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            moviePlayerUIControl1.CloseInterfaces();
            moviePlayerUIControl2.CloseInterfaces();
        }

        #endregion

        #region Controls

        /// <summary>
        /// Set root folder
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void setRootFolderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.SelectedPath = App.Default.RootPath;

            DialogResult result = folderBrowserDialog1.ShowDialog();

            if (result == DialogResult.OK)
            {
                App.Default.RootPath = folderBrowserDialog1.SelectedPath;
                App.Default.Save();

                patients1.LoadPatientXML();
            }
        }

        /// <summary>
        /// Show the exercise form.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void manageExercisesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            exerciseManagement.Show();
        }

        #endregion

        #region Support

        /// <summary>
        /// Assign playback videos
        /// </summary>
        public void AssignVideoDropdowns()
        {
            ComboBox playbackLists = ((ComboBox)gbCameraPlayback1.Controls["cbPlayback1"]);
            ComboBox playbackLists2 = ((ComboBox)gbCameraPlayback2.Controls["cbPlayback2"]);

            playbackLists.Items.Clear();
            playbackLists2.Items.Clear();
            string[] videofiles = Directory.GetFiles(GlobalVariables.VideoPath, "*.wmv", SearchOption.TopDirectoryOnly);
            for (int i = 0; i < videofiles.Length; i++)
            {
                string[] s = videofiles[i].Split('\\');
                playbackLists.Items.Add(new ListItem(s[s.Length - 1], videofiles[i]));
                playbackLists2.Items.Add(new ListItem(s[s.Length - 1], videofiles[i]));
            }
        }

        #endregion
    }
}
